<?php defined('SYSPATH') or die('No direct script access.');

class Track {

	protected $folder;

	protected $event_type;
	protected $http_user_agent;
	protected $file_name;
	protected $ip;
	protected $source_id;
	protected $hashcookie;
	protected $hash;
	protected $file_format;
	protected $recipient_id;
	protected $url;
	protected $file_extension;

	public function __construct()
	{
		$this->url = Kohana::$config->load('track')->url;
		$this->hash = kohana::$config->load('track')->hash;
		$this->file_extension = Kohana::$config->load('track')->file_extension;
		$this->file_format = Kohana::$config->load('track')->file_format;
		if(substr(kohana::$config->load('track')->folder, -1, 1)!='/') {
			$this->folder = kohana::$config->load('track')->folder.'/';
		} else {
			$this->folder = kohana::$config->load('track')->folder;
		}
		$this->source_id = kohana::$config->load('track')->source_id;
		$this->ip = @$_SERVER['REMOTE_ADDR'];
		$this->http_user_agent = @$_SERVER['HTTP_USER_AGENT'];
		$this->dt_event = date("Y-m-d H:i:s");
		$this->file_name = "queue.track.".str_replace(' ','_',date($this->file_format));
	}

	public function saveFileEvent($childArrayData=array())
	{
		$data = array(
				'source_id' => $this->source_id,
				'event_type' => $this->event_type,
				'dt_event' => $this->dt_event,
				'hash' => $this->hash,
				'ip' => $this->ip,
				'http_user_agent' => $this->http_user_agent,
				'hashcookie' => $this->hashcookie,
				'recipient_id' => $this->recipient_id,
		);
		foreach($childArrayData as $key=>$value){
			$data[$key] = $value;
		}
		if(!is_dir($this->folder)) {
			$pathArray = explode("/", $this->folder);
			$partial_path = "/";
			foreach($pathArray as $folder) {
				$partial_path .= $folder."/";
				if(!is_dir($partial_path)){
					mkdir($partial_path);
				}
			}
		}
		$path = $this->folder.$this->file_name.'.'.$this->file_extension;
		$writeData = implode('|',$data).PHP_EOL;
		$handle = fopen($path,"a");
		fwrite($handle, $writeData);
		fclose($handle);
	}

	public function doTrack($childArrayData=array())
	{
		$getData = array(
				'source_id' => $this->source_id,
				'event_type' => $this->event_type,
				'dt_event' => $this->dt_event,
				'hash' => $this->hash,
				'ip' => $this->ip,
				'http_user_agent' => $this->http_user_agent,
				'hashcookie' => $this->hashcookie,
				'recipient_id' => $this->recipient_id,
		);
		foreach($childArrayData as $key=>$value){
			$getData[$key] = $value;
		}
		$getData = str_replace(" ","%20", implode('|', $getData));

		$defaults = array(
				CURLOPT_URL => $this->url."?tc=".$getData,
				CURLOPT_HEADER => 0,
				CURLOPT_RETURNTRANSFER => TRUE,
				CURLOPT_TIMEOUT => 4
			);
		$curl_connection = curl_init();
		curl_setopt_array($curl_connection, $defaults);

		if( ! $result = curl_exec($curl_connection)){
			$this->saveFileEvent($childArrayData);
		}

		curl_close($curl_connection);

	}
}

//		Call via file_get_contents
//
//		$getdata = http_build_query(
//			array(
//				'hash' => 'asdasdasdasdasdasd',
//				'recipient_id' => '1',
//				'categories' => '23,127',
//				'subcategories' => '87,342'
//			)
//		);
//		$opts = array('http' =>
//			array(
//				'protocol_version'=>'HTTP/1.1',
//				'method'  => 'GET /click',
//				'header'  => 'Host: localhost/kotarget\nContent-type: application/x-www-form-urlencoded\n\n',
//				'content' => $getdata
//			)
//		);
//		$context  = stream_context_create($opts);
//		$url = "http://localhost/kotarget/click";
//		$result = file_get_contents($url, false, $context);